<?php

class Stylist extends My_Controller {

	public function __construct(){
		parent::__construct();
		$this->layout_view = 'stylist';
		if(!$this->session->userdata('stylist_logged_in'))
			redirect('login/stylist');
	}
	
	public function logout()
	{
		$this->session->sess_destroy();
		redirect('login/stylist');	
	}
	
	public function index(){

	}
	
	public function my_profile(){
		$this->title = 'My Profile';	
		$this->load->model('M_users');
		
	}
	
	public function edit_profile() {
		$this->title = 'Edit Profile';
		$this->load->model('M_users');
		$this->data['user'] = $this->M_users->getUser($this->session->userdata('stylist_id'));
		
		if($this->input->post('save'))
		{
			$this->form_validation->set_rules('username', 'Username', 'required');
			$this->form_validation->set_rules('firstname', 'Firstname', 'required');
			$this->form_validation->set_rules('lastname', 'Lastname', 'required');
			$this->form_validation->set_rules('contact', 'Contact', 'required|integer');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			if ($this->form_validation->run() == FALSE)
			{
				// do nothing	
			}
			else 
			{
				$data = clean_post('save', $this->input->post());	
				if($this->M_users->updateUser($data, $this->session->userdata('stylist_id')))
					set_redirect('success', 'User updated successfully', 'stylist/edit_profile/');
				else
					set_redirect('error', 'Oops! something went wrong, please try again later', 'stylist/edit_profile/');
			}

		}
		
	}
	
	public function change_password(){
		$this->title = 'Change Passsword';
		if($this->input->post('save'))
		{
			$this->form_validation->set_rules('current_password', 'Current Password', 'required');
			$this->form_validation->set_rules('new_password', 'New Password', 'required');
			$this->form_validation->set_rules('r_new_password', 'Repeat New Password', 'required|matches[new_password]');
			
			if($this->form_validation->run() == FALSE)
			{
				
			}
			else
			{
				
				$this->load->model('M_users');	
				if($this->M_users->verify_password($this->input->post('current_password'), $this->session->userdata('stylist_id')) == TRUE)
				{
					
					if($this->M_users->change_pass($this->input->post('new_password'),$this->session->userdata('stylist_id')) == TRUE)
						set_redirect('success', 'Password changed successfully', 'stylist/change_password');
					else
						set_redirect('notice', 'Cannot change password at this moment please try again later.', 'stylist/change_password');
						
				}
				else
				{
					set_redirect('error', 'Your current password does not match the one our database', 'stylist/change_password');
				}	
			}
			
		}	
	}
	
	public function booking_appointments(){
		$this->title = 'Booking Appointments';
		
		$this->load->model('M_bookings');
		$this->data['bookings'] = $this->M_bookings->getStylistBookings($this->session->userdata('stylist_id'));
	}
	
	
	public function chat(){
		$this->title = 'Chat';
		$this->data['booking_id'] = $this->uri->segment(3);
		$this->load->model('M_users');
		$this->load->model('M_bookings');
		$booking = $this->M_bookings->getBooking($this->uri->segment(3));
		
		$datetime1 = date('Y-m-d H:i:s', strtotime($booking->adate.' '.$booking->aps_time_from));
		$current = new DateTime(date('Y-m-d H:i:s', strtotime(date('Y-m-d g:i A'))));

		$check1 = strtotime(date('Y-m-d H:i:s')) >= strtotime($booking->adate.' '.$booking->aps_time_from) ? TRUE : FALSE;
		$check2 = strtotime(date('Y-m-d H:i:s')) <= strtotime($booking->adate.' '.$booking->aps_time_to) ? TRUE : FALSE;
		
		if(!($check1 && $check2)){
			$this->data['disabled']	= TRUE;
		}
			
		$this->data['client'] = $this->M_users->getUser($this->uri->segment(4));
	}
}

?>